package com.yeu.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.smslib.AGateway;
import org.smslib.GatewayException;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.TimeoutException;
import org.smslib.modem.SerialModemGateway;
import org.smslib.ICallNotification;
import org.smslib.IInboundMessageNotification;
import org.smslib.InboundMessage;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.Message.MessageTypes;

public class SMSTool {
	private static Service srv ;
	private static SerialModemGateway gateway;
	private static STATE openState=STATE.OPEN;
	private static STATE sendState=STATE.NORMAL;
	private static STATE readState=STATE.NORMAL;
	
	public static STATE getOpenState() {
		return openState;
	}

	public static STATE getSendState() {
		return sendState;
	}

	public static STATE getReadState() {
		return readState;
	}

	public static List<HashMap<String,String>> recvMsg = new ArrayList<HashMap<String,String>>();
	
	public static enum STATE{
		OPEN,CLOSE,ERROR,SENDING,READING,NORMAL;
	}
	
	
	static{
		srv=Service.getInstance();
		String id = ConfigurationReader.get("id");
		String comPort = ConfigurationReader.get("comPort");
		int baudRate = Integer.valueOf(ConfigurationReader.get("baudRate"));
		String manufacturer = ConfigurationReader.get("manufacturer");
		String model = ConfigurationReader.get("model");
		
		gateway=new SerialModemGateway(id, comPort, baudRate, manufacturer, model);
		
		gateway.setInbound(true);
		gateway.setOutbound(true);
		gateway.setSimPin("0000");
		
		try {
			srv.addGateway(gateway);
			srv.startService();
		} catch (SMSLibException | IOException | InterruptedException e) {
			// 短信猫服务开启失败
			System.out.println("服务开启失败");
			SMSTool.openState=SMSTool.STATE.CLOSE;
			e.printStackTrace();
		}
		
	}
	/**
	 * 发送单条信息
	 * @param addr
	 * @param text
	 * @return
	 */
	public static boolean sendMessage(String addr,String text){
		sendState=STATE.SENDING;
		boolean flag=false;
		OutboundMessage msg = new OutboundMessage(addr, text);
		msg.setEncoding(MessageEncodings.ENCUCS2);
		try {
			flag=srv.sendMessage(msg);
		} catch (TimeoutException | GatewayException | IOException
				| InterruptedException e) {

			e.printStackTrace();
			System.out.println(addr+"信息发送失败");
			return false;
		}
		sendState=STATE.NORMAL;
		return flag;
		
		
	}
	
	/**
	 * 发送多条信息
	 * @param addrs
	 * @param text
	 * @return
	 */
	public static List<String> sendMessages(List<String> addrs,String text){
		sendState=STATE.SENDING;
		List<String> errList = new ArrayList<String>();
		boolean flag=false;
		for (String addr : addrs) {
			OutboundMessage msg = new OutboundMessage(addr,text);
			msg.setEncoding(MessageEncodings.ENCUCS2);
			try {
				flag=srv.sendMessage(msg);
			} catch (TimeoutException | GatewayException | IOException
					| InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
//				errList.add(addr);
				System.out.println(addr+"信息发送失败");
			}
			if(flag==false)errList.add(addr);
		}
		sendState=STATE.NORMAL;
		return errList;
	}
	
	/**
	 * 关闭服务
	 */
	public static void stop(){
		System.out.println("进行关闭操作");
		try {
			openState=STATE.CLOSE;
			srv.stopService();
			gateway.stopGateway();
			System.out.println("关闭操作结束");
		} catch (SMSLibException | IOException | InterruptedException e) {
			// TODO Auto-generated catch block
			System.out.println("服务关闭失败");
			e.printStackTrace();
		}
		
	}
	
	public static void readMessage(){
		readState=STATE.READING;
		Thread t = new Thread(){
			
		HashMap<String,String> msgMap;
			@Override
			public void run() {
				List<InboundMessage> msgList = new ArrayList<InboundMessage>();
				
				try {
					srv.readMessages(msgList, MessageClasses.UNREAD);
				} catch (TimeoutException | GatewayException | IOException
						| InterruptedException e) {
					e.printStackTrace();
					System.out.println("读取信息过程失败");
				}
				for (InboundMessage msg : msgList) {
					msgMap=new HashMap<String, String>();
					msgMap.put("addr", msg.getOriginator());
					msgMap.put("date", msg.getDate().toString());
					msgMap.put("text", msg.getText());
					recvMsg.add(msgMap);
					
					try {
						srv.deleteMessage(msg);
					} catch (TimeoutException | GatewayException | IOException
							| InterruptedException e) {
						e.printStackTrace();
						System.out.println("短信删除失败");
					}
				}
				readState=STATE.NORMAL;
			}
			
		};
		t.start();
		
	}
	
	
	
	
	public static void main(String[] args) {
		List<String> testAddrs = new ArrayList<String>();
		testAddrs.add("15805003765");
//		testAddrs.add("15396377103");
//		testAddrs.add("15559189010");
//		testAddrs.add("15559127165");
//		SMSTool.readMessage();
//		SMSTool.sendMessages(testAddrs, "封装结果测试，线程1短信群发，线程2短信接收");
		SMSTool.sendMessage("15805003765", "haha");
//		while(true){
//			if(readState==STATE.READING)continue;
//			else{ 
//				System.out.println(recvMsg);
//				break;
//			}
//		}
		stop();
		System.out.println("程序结束");
		System.exit(0);
	}
}
